
from django.utils import timezone
from django.db import models

# Create your models here.

class Articlecategory(models.Model):
    title = models.CharField(max_length=100,blank=True)

    create_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.title

    class Meta:
        db_table = 'articlecategory'
        verbose_name = '分类管理'
        verbose_name_plural = verbose_name

class Article(models.Model):
    # 定义文章作者字段，使用外键关联用户模型，当关联的用户被删除时，级联删除文章
    author = models.ForeignKey('user.User', on_delete=models.CASCADE)

    # 定义用户头像字段，使用ImageField字段类型，可以存储图片文件路径
    # upload_to参数指定文件保存路径的前缀，%Y%m%d表示使用年月日日期格式动态生成子目录
    # blank=True允许该字段在表单中为空
    avatar = models.ImageField(upload_to='media/avatar/%Y%m%d/', blank=True)

    # 定义文章分类字段，使用外键关联文章分类模型，允许为空，当关联的分类被删除时，级联删除文章
    # related_name参数指定反向关联的名称，可以通过article访问关联的文章实例
    category = models.ForeignKey('Articlecategory', null=True, blank=True, on_delete=models.CASCADE,related_name='article')
    tags = models.CharField(max_length=100,blank=True)
    title = models.CharField(max_length=100,blank=False)
    sumary = models.TextField(max_length=500,blank=False)
    content = models.TextField(max_length=10000,blank=False)
    # 文章评论数
    comments_count = models.PositiveIntegerField(default=0)
    updated = models.DateTimeField(auto_now=True)
    total_views = models.IntegerField(default=0,blank=True)
    class Meta:
        db_table = 'article'
        ordering = ['-updated']
        verbose_name = '文章管理'
        verbose_name_plural = verbose_name
        def __str__(self):
            return self.title

class Comment(models.Model):
    content = models.TextField(max_length=10000,blank=False)
    article = models.ForeignKey('Article', on_delete=models.CASCADE,null=True)
    user = models.ForeignKey('user.User', on_delete=models.CASCADE,null=True)
    created=models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.content
    class Meta:
        db_table = 'comment'
        ordering = ['-created']
        verbose_name = '评论管理'
        verbose_name_plural = verbose_name